package tiled.mapeditor.util;

import java.awt.*;
import java.util.LinkedList;
import java.util.ListIterator;

public class MapEventAdapter
{
    public static final int ME_MAPACTIVE = 1;
    public static final int ME_MAPINACTIVE = 2;

    private LinkedList listeners;

    public MapEventAdapter()
    {
        listeners = new LinkedList();
    }

    public void addListener(Component obj)
    {
        if (listeners.indexOf(obj) == -1)
        {
            listeners.add(obj);
        }
    }

    public void removeListener(Component obj)
    {
        listeners.remove(obj);
    }

    public void fireEvent(int type)
    {
        if (type == ME_MAPACTIVE)
        {
            enableEvent();
        }
        else if (type == ME_MAPINACTIVE)
        {
            disableEvent();
        }
    }

    private void enableEvent()
    {
        Component c;
        ListIterator li = listeners.listIterator();
        while(li.hasNext())
        {
            c = (Component)li.next();
            c.setEnabled(true);
        }
    }

    private void disableEvent()
    {
        Component c;
        ListIterator li = listeners.listIterator();
        while(li.hasNext())
        {
            c = (Component)li.next();
            c.setEnabled(false);
        }
    }
}
